package com.example.swd1.gudong.main.user.systemoption;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.swd1.gudong.R;

/**
 * Created by user on 9/7/16.
 */
public class OptionSwitchPreference extends Preference {
    private String key;
    private String text;
    private String summary;
    private ImageView iv_option_switch_bg_on;
    private ImageView iv_option_switch_bg_off;
    private ImageView iv_option_switch_bt_left;
    private ImageView iv_option_switch_bt_right;
    private RelativeLayout rl_option_switch;
    private boolean buttonChecked;
    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switchCLick();
        }
    };


    @Override
    public SharedPreferences getSharedPreferences() {
        return super.getSharedPreferences();
    }


    public OptionSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SwitchPreference);
        text = a.getString(R.styleable.SwitchPreference_switch_text);
        summary = a.getString(R.styleable.SwitchPreference_summary);
        key = a.getString(R.styleable.SwitchPreference_key);
        a.recycle();
    }

    public OptionSwitchPreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    @Override
    protected void onBindView(View view) {
        super.onBindView(view);
        TextView textView = (TextView) view.findViewById(R.id.tv_option_switch_text);
        TextView summaryView = (TextView) view.findViewById(R.id.tv_option_switch_summary);
        iv_option_switch_bg_on = (ImageView) view.findViewById(R.id.iv_option_switch_bg_on);
        iv_option_switch_bg_off = (ImageView) view.findViewById(R.id.iv_option_switch_bg_off);
        iv_option_switch_bt_left = (ImageView) view.findViewById(R.id.iv_option_switch_bt_left);
        iv_option_switch_bt_right = (ImageView) view.findViewById(R.id.iv_option_switch_bt_right);
        rl_option_switch = (RelativeLayout) view.findViewById(R.id.rl_option_switch);
        textView.setText(text);
        summaryView.setText(summary);
        buttonChecked = obtainKeyButtonCheckStatue();
        setButtonChecked(buttonChecked);
        rl_option_switch.setOnClickListener(listener);
    }

    @SuppressLint("MissingSuperCall")
    @Override
    protected View onCreateView(ViewGroup parent) {
        return LayoutInflater.from(getContext()).inflate(R.layout.preference_option_switch, parent, false);
    }


    public void setButtonChecked(boolean buttonChecked) {
        if (buttonChecked) {
            //button on
            iv_option_switch_bg_on.setVisibility(View.VISIBLE);
            iv_option_switch_bg_off.setVisibility(View.INVISIBLE);
            iv_option_switch_bt_left.setVisibility(View.INVISIBLE);
            iv_option_switch_bt_right.setVisibility(View.VISIBLE);
        } else {
            //button off
            iv_option_switch_bg_on.setVisibility(View.INVISIBLE);
            iv_option_switch_bg_off.setVisibility(View.VISIBLE);
            iv_option_switch_bt_left.setVisibility(View.VISIBLE);
            iv_option_switch_bt_right.setVisibility(View.INVISIBLE);
        }
    }


    private void switchCLick() {
        buttonChecked = !buttonChecked;
        if (buttonChecked) {
            setButtonChecked(true);
        } else {
            setButtonChecked(false);
        }
        saveKeyButtonCheckStatue();
    }

    public boolean obtainKeyButtonCheckStatue() {
        return getSharedPreferences().getBoolean(key, false);
    }

    public void saveKeyButtonCheckStatue() {
        SharedPreferences sharedPreferences = getSharedPreferences();
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean(key, buttonChecked);
        edit.commit();
    }

}
